$(function () {
    var layer = layui.layer;
    getuserInfo()
    $('#logout').on('click', function () {
        layer.confirm('您确定退出吗?', { icon: 3, title: '提示' }, function (index) {
            localStorage.removeItem('token');
            location.href = 'login.html'
            layer.close(index);//关闭当前层
        })
    })
})
function getuserInfo() {
    $.ajax({
        type: "get",
        url: "/my/userinfo",
        success: function (res) {
            if (res.status !== 0) {
                return layer.msg(res.massage)
            }
            renderAvatar(res.data);
        }
    })
}
function renderAvatar(user) {
    var name = user.nickname || user.username
    $('#welcome').html('欢迎 ' + name)
    if (user.user_pic) {
        $('.layui-nav-img').prop('src', user.user_pic)
        $('.text-avatar').hide();
    } else {
        var first = name[0]
        $('.text-avatar').html(first.toUpperCase());
        $('.layui-nav-img').hide();
    }
}